# -*- Mode: Python -*- vi:si:et:sw=4:sts=4:ts=4:syntax=python

from cerbero.tools.libtool import LibtoolLibrary

class Recipe(recipe.Recipe):
    name = 'openh264'
    version = '1.7.0'
    url = 'https://github.com/cisco/%(name)s/archive/v%(version)s.tar.gz'
    tarball_checksum = '9c07c38d7de00046c9c52b12c76a2af7648b70d05bd5460c8b67f6895738653f'
    stype = SourceType.TARBALL
    btype = BuildType.MAKEFILE
    licenses = [{License.BSD: ['LICENSE']}]
    files_libs = ['libopenh264']
    files_devel = ['include/wels', 'lib/pkgconfig/openh264.pc']
    patches = ['openh264/0001-openh264-Build-for-8.0-fembed-bitcode.patch',
               'openh264/0002-ios-Remove-flags-incompatible-with-fembed-bitcode.patch',
               'openh264/0001-android-build-switch-to-using-the-c-_shared-c-librar.patch']

    def prepare(self):
        make = self.make
        if self.config.target_platform == Platform.IOS:
            make += ['OS=ios']
            if self.config.target_arch == Architecture.X86:
                make += ['ARCH=i386']
            elif self.config.target_arch == Architecture.X86_64:
                make += ['ARCH=x86_64']
            elif self.config.target_arch == Architecture.ARMv7:
                make += ['ARCH=armv7']
            elif self.config.target_arch == Architecture.ARMv7S:
                make += ['ARCH=armv7s']
            elif self.config.target_arch == Architecture.ARM:
                make += ['ARCH=arm', 'APP_ABI=armeabi']
            elif self.config.target_arch == Architecture.ARM64:
                make += ['ARCH=arm64']
        elif self.config.target_platform == Platform.DARWIN:
            make += ['OS=darwin']
            if self.config.target_arch == Architecture.X86:
                make += ['ARCH=x86']
            elif self.config.target_arch == Architecture.X86_64:
                make += ['ARCH=x86_64']
        elif self.config.target_platform == Platform.ANDROID:
            v = DistroVersion.get_android_api_version(self.config.target_distro_version)
            make += ['OS=android', 'TARGET=android-' + str(v), 'NDKLEVEL=' + str(v)]
            make += ['NDKROOT=' + self.config.toolchain_prefix]
            if self.config.target_arch == Architecture.X86:
                # According to https://github.com/cisco/openh264/issues/2263 
                # need to pass ENABLEPIC=Yes to fix text relocations
                make += ['ARCH=x86', 'ENABLEPIC=Yes']
            elif self.config.target_arch == Architecture.ARM64:
                make += ['ARCH=arm64']
            elif self.config.target_arch == Architecture.X86_64:
                make += ['ARCH=x86_64']
        elif self.config.target_platform == Platform.WINDOWS:
            make += ['OS=mingw_nt']
            if self.config.target_arch == Architecture.X86:
                make += ['ARCH=x86']
            elif self.config.target_arch == Architecture.X86_64:
                make += ['ARCH=x86_64']
        elif self.config.target_platform == Platform.LINUX:
            if self.config.target_arch == Architecture.ARMv7:
                make += ['ARCH=armv7']
            elif self.config.target_arch == Architecture.ARMv7S:
                make += ['ARCH=armv7s']
            elif self.config.target_arch == Architecture.ARM:
                make += ['ARCH=arm', 'APP_ABI=armeabi']
            elif self.config.target_arch == Architecture.ARM64:
                make += ['ARCH=arm64']

        self.make = make + ['libraries']
        # `make install` also needs the exact same parameters as `make`
        self.make_install = make + ['install']

    def configure(self):
        if self.config.platform == Platform.WINDOWS:
            # Convert c:/foo to /c/foo for this build system. It will add the
            # initial / by itself, /c:/foo does not work and and //c is not
            # the same as /c on Windows. //c/foo is a network path.
            if not self.config.prefix.startswith('/') and self.config.prefix[1] == ':':
                p = "/" + self.config.prefix[0] + self.config.prefix[2:]
            else:
                p = self.config.prefix
            shell.replace(os.path.join(self.build_dir, 'Makefile'),
                          # Fix hard-coded prefix
                          {'PREFIX=/usr/local': "PREFIX=" + p})
        else:
            shell.replace(os.path.join(self.build_dir, 'Makefile'),
                          # Fix hard-coded prefix
                          {'PREFIX=/usr/local': "PREFIX=" + self.config.prefix})

        if self.config.target_platform == Platform.IOS and self.config.target_arch in [Architecture.X86, Architecture.X86_64]:
            shell.replace(os.path.join(self.build_dir, 'build',
                                       'platform-ios.mk'),
                          # Use correct compiler flag for iOS simulator
                          {'-miphoneos-version-min': '-mios-simulator-version-min'})

        # No configure script

    def post_install(self):
        # XXX: Don't forget to keep this in sync with the library version!
        dependency_libs=[]
        if self.config.target_platform == Platform.IOS:
            dependency_libs += ['-lc++']
        elif self.config.target_platform == Platform.DARWIN:
            dependency_libs += ['-lc++']
        elif self.config.target_platform == Platform.ANDROID:
            dependency_libs += ['gnustl', '-lm']
        elif self.config.target_platform in [Platform.WINDOWS, Platform.LINUX]:
            dependency_libs += ['-lstdc++', '-lm']
        else:
            raise NotImplementedError

        libtool_la = LibtoolLibrary('openh264', 0, None, None, self.config.libdir,
                                    self.config.target_platform,
                                    deps=dependency_libs)
        libtool_la.save()
        super().post_install()
